package controller

import (
	"github.com/kataras/iris/v12"
	"go-movies/model"
	"go-movies/util"
	"strconv"
)

/**
 * GetMovieWithCount: 获取电影信息， 分页获取
 */
func GetMovieWithCount(ctx iris.Context){
	limit, offset, isOk := util.ParserPageCount(ctx)
	if !isOk {
		model.HandleErrorModel(ctx, 406, "System prompt: request parameter type resolution error")
		return
	}
	if movies, count, ok := model.GetMovie(limit, offset); ok {
		data := &struct{
			Movies []model.MovieInfo  `json:"movies"`
			Count int `json:"count"`
		}{
			Movies: movies,
			Count: count,
		}
		model.HandleSuccessModel(ctx, 200, data, "System prompt: obtain data successfully")
		return
	}else{
		model.HandleErrorModel(ctx, 500, "System prompt: service query data failed, please try again")
		return
	}
}

/**
 * GetMovieWithType: 根据电影类型获取电影数据
 */
func GetMovieWithType(ctx iris.Context){
	// 解析出类型:
	types := ctx.Params().Get("type")
	limit, offset, isOk := util.ParserPageCount(ctx)
	if !isOk {
		model.HandleErrorModel(ctx, 406, "System prompt: error in request parameter passing")
		return
	}
	if movies, count, ok := model.GetTypeMovie("movie_type", types, limit, offset); ok {
		data := struct{
			Movies []model.MovieInfo `json:"movies"`
			Count int `json:"count"`
		}{
			Movies: movies,
			Count: count,
		}
		model.HandleSuccessModel(ctx, 200,  data, "System prompt: query data successfully")
		return
	}else{
		model.HandleErrorModel(ctx, 500, "System prompt: Failed to query data")
		return
	}
}

/**
 * 根据电影名模糊查询电影，并带分页效果
 */
func SearchMovieWithTitle(ctx iris.Context){
	q := ctx.URLParam("q")
	limit, offset, isOk := util.ParserPageCount(ctx)
	if !isOk {
		model.HandleErrorModel(ctx, 406, "System tip: error parsing query parameters")
		return
	}
	if movies, count, ok := model.GetTypeMovie("movie_name", q, limit, offset); ok {
		data := struct{
			Movies []model.MovieInfo `json:"movies"`
			Count int `json:"count"`
		}{
			Movies: movies,
			Count: count,
		}
		model.HandleSuccessModel(ctx, 200,  data, "System prompt: query data successfully")
		return
	}else{
		model.HandleErrorModel(ctx, 500, "System prompt: Failed to query data")
		return
	}
}

/**
 * LikeMovieToUser： 收藏该电影
 */
func LikeMovieToUser(ctx iris.Context) {
	id, err := strconv.Atoi(ctx.Params().Get("id"))
	if err != nil {
		model.HandleErrorModel(ctx, 406, "System tip: error parsing movie information, please check the upload")
		return
	}
	// 执行更新:
	username := util.GetJWTValue(ctx, "username")
	if model.CollectionMovie(id, username) {
		model.HandleSuccessModel(ctx, 200, "ok", "System tip: successful movie collection")
		return
	}else{
		model.HandleErrorModel(ctx, 500, "System tip: Movie collection failed")
	}
}

/**
 * GetMovieInfo: 获取电影详情
 */
func GetMovieInfo(ctx iris.Context){
	// 解析出电影ID:
	movieId := ctx.Params().Get("id")
	id, err := strconv.Atoi(movieId)
	if err != nil {
		model.HandleErrorModel(ctx, 406, "System tip: error parsing query parameters")
		return
	}
	// 获取电影：
	if movie, isOk := model.GetMovieInfo(id); isOk {
		data := &struct{
			Movie model.MovieInfo `json:"movie"`
		}{
			Movie: movie,
		}
		model.HandleSuccessModel(ctx, 200, data, "System prompt: query data successfully")
		return
	}else{
		model.HandleErrorModel(ctx, 500, "System prompt: Failed to query data")
		return
	}
}